package com.cn.aam.checaiduo.ui.homepage.adapter;

import android.content.Context;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.cn.aam.checaiduo.R;
import com.cn.aam.checaiduo.base.adapter.BaseRecycerAdapter;
import com.cn.aam.checaiduo.base.adapter.holder.BaseRecyclerViewHolder;
import com.cn.aam.checaiduo.bean.ProductItemB;
import com.cn.aam.checaiduo.utils.MyViewsUtils;
import com.cn.aam.checaiduo.widget.CircleProgressBarView;
import com.orhanobut.logger.Logger;

import butterknife.BindView;
import me.grantland.widget.AutofitHelper;

/**
 * @author 艹羊
 * @project Checaiduo_Android
 * @date 2017/5/31 上午9:36
 * @description 产品列表适配器
 */

public class CrowdFundAdapter extends BaseRecycerAdapter<ProductItemB> {


    public CrowdFundAdapter(Context mContext) {
        super(mContext);
    }


    @Override
    protected BaseRecyclerViewHolder getViewHolder(View picNewsView) {
        return new ViewHolder(picNewsView);
    }


    @Override
    protected int getItemLayoutRes() {
        return R.layout.list_item_crowd_fund;
    }

    @Override
    protected void ItemVIewChange(BaseRecyclerViewHolder recyclerholder, final int position) {
        ViewHolder holder = (ViewHolder) recyclerholder;
        ProductItemB item = getItem(position);
        //click
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onItemClick != null)
                    onItemClick.onClick(position);
            }
        });
        //title
        holder.tvTitle.setText("第" + item.getSequence() + "期 " + item.getTitle());
        //progress
        holder.progressbar.setProgressValue(item.getProgress());
        //identity
        showIdentity(holder, item.getNew_invester(), item.getVip_invester());
        //status
        showStatus(holder, item);
    }

    /**
     * @param holder       视图持有者
     * @param new_invester 是否为新投资者
     * @param vip_invester 是否为vip
     * @description 产品的定位
     */
    private void showIdentity(ViewHolder holder, String new_invester, String vip_invester) {
        if (TextUtils.equals("1", new_invester))
            MyViewsUtils.showAllViews(holder.ivIdentityShort);
        if (TextUtils.equals("1", vip_invester))
            MyViewsUtils.showAllViews(holder.ivIdentityVip);
        if (TextUtils.equals("0", new_invester) && TextUtils.equals("0", vip_invester))
            MyViewsUtils.showAllViews(holder.ivIdentityDefault);
        if (TextUtils.isEmpty(new_invester) && TextUtils.isEmpty(vip_invester))
            MyViewsUtils.showAllViews(holder.ivIdentityDefault);
    }

    private static final int COLOR_RED = Color.parseColor("#F93A3A");
    private static final int COLOR_PURPLE = Color.parseColor("#5B5F97");
    private static final int COLOR_YELLOW = Color.parseColor("#FFC145");

    /**
     * @param holder 视图持有者
     * @param item   条目
     * @description 产品的状态
     */
    private void showStatus(ViewHolder holder, ProductItemB item) {
        String status_code = item.getStatus_code();
        TextView tvStatus = holder.tvStatus;

        TextView tvTipAnnualYield = holder.tvTipAnnualYield;
        TextView tvAnnualYield = holder.tvAnnualYield;

        switch (status_code) {
            case "1"://募集中
                //annual yield
                tvTipAnnualYield.setText("预期年化收益");
                tvAnnualYield.setText("≥" + item.getExpect_yield_rate() + "%");

                //status
                tvStatus.setText("募集中");
                tvStatus.setTextColor(COLOR_RED);
                tvStatus.setBackgroundResource(R.drawable.bg_product_status_red);

                //background
                holder.itemView.setBackgroundColor(Color.WHITE);
                //expired day
                holder.tvDayReturnMoney.setText("≤" + item.getExpired_day() + "天");

                MyViewsUtils.hideAllViews(holder.blockProfitWan);
                MyViewsUtils.showAllViews(holder.blockProgressbar);
                break;
            case "21"://起息中
                //annual yield
                tvTipAnnualYield.setText("预期年化收益");
                tvAnnualYield.setText("≥" + item.getExpect_yield_rate() + "%");

                //status
                tvStatus.setText("起息中");
                tvStatus.setTextColor(COLOR_PURPLE);
                tvStatus.setBackgroundResource(R.drawable.bg_product_status_purple);

                //background
                holder.itemView.setBackgroundColor(Color.WHITE);
                //expired day
                holder.tvDayReturnMoney.setText("≤" + item.getExpired_day() + "天");

                MyViewsUtils.hideAllViews(holder.blockProfitWan);
                MyViewsUtils.showAllViews(holder.blockProgressbar);
                break;
            case "41"://已回款
                //annual yield
                tvTipAnnualYield.setText("单利年化");
                tvAnnualYield.setText("" + item.getSi_year_rate() + "%");

                //status
                tvStatus.setText("已回款");
                tvStatus.setTextColor(COLOR_YELLOW);
                tvStatus.setBackgroundResource(R.drawable.bg_product_status_yellow);

                //background
                holder.itemView.setBackgroundColor(Color.argb(255, 245, 245, 248));

                //expired day
                holder.tvDayReturnMoney.setText("" + item.getDiff_day() + "天");
                holder.tvProfitWan.setText("" + item.getWan_yield() + "元");

                MyViewsUtils.showAllViews(holder.blockProfitWan);
                MyViewsUtils.hideAllViews(holder.blockProgressbar);
                break;
        }
    }


    public class ViewHolder extends BaseRecyclerViewHolder {
        //title
        @BindView(R.id.tv_title)
        TextView tvTitle;

        //block
        @BindView(R.id.block_profit_wan)
        LinearLayout blockProfitWan;//万元收益
        @BindView(R.id.block_progressbar)
        FrameLayout blockProgressbar;//进度条


        @BindView(R.id.tv_tip_annual_yield)
        TextView tvTipAnnualYield;
        //center content
        @BindView(R.id.tv_annual_yield)
        TextView tvAnnualYield;
        @BindView(R.id.tv_day_return_money)
        TextView tvDayReturnMoney;
        @BindView(R.id.tv_profit_wan)
        TextView tvProfitWan;

        @BindView(R.id.progressbar)
        CircleProgressBarView progressbar;


        //status
        @BindView(R.id.tv_status)
        TextView tvStatus;

        //identity
        @BindView(R.id.iv_identity_default)
        ImageView ivIdentityDefault;
        @BindView(R.id.iv_identity_short)
        ImageView ivIdentityShort;
        @BindView(R.id.iv_identity_vip)
        ImageView ivIdentityVip;

        public ViewHolder(View itemView) {
            super(itemView);
//            AutofitHelper.create(tvTitle);//使其可以自动伸缩
            MyViewsUtils.hideAllViews(ivIdentityDefault, ivIdentityShort, ivIdentityVip);
        }
    }
}
